﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SalePartial.BLL;
using SalePartial.Model;
using System.Data.SqlClient;

public partial class NewsContent : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Guid newsid = Guid.Empty;
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != string.Empty)
            {
                newsid = new Guid (Request.QueryString["id"]);
            }
            XPNews nm = new XPNews();
            XPNewsInfo n = nm.GetXPNews(newsid);
            litTitle.Text = n.Title;
            litContent.Text = n.Content;
            litNewsTime.Text = n.CreateTime.ToShortDateString();
            bindToComment(newsid);

        }
    }

    private void bindToComment(Guid newsid)
    {
        XPNewsComment cm = new XPNewsComment();
        //SqlDataReader reader = cm.SelectByNewsID(newsid);
        //if (!reader.HasRows)
       DataSet ds = cm.SelectDynamic("NewsID='"+newsid+"'","CreateDate DESC");
       if (ds == null || ds.Tables[0].Rows.Count==0)
        {
            //无评论
            this.emptypl.Visible = true;
        }
        else
        {
            this.emptypl.Visible = false;
            rptComment.DataSource = ds;
            rptComment.DataBind();
        }
    }
    //删除
    protected void LbtnDel_Click(object sender, EventArgs e)
    {
        LinkButton lb = (LinkButton)sender;
        XPNewsComment cm = new XPNewsComment();
        if (lb.CommandArgument != null && lb.CommandArgument != string.Empty)
        {
            try
            {
                XPNewsCommentInfo info = new XPNewsCommentInfo();
                info.CoID = new Guid(lb.CommandArgument);

                cm.Delete(info);
            }
            catch (Exception)
            {
                Response.Write("<Script language='javascript'>alert('删除失敗');</script>");
                return;
            }
            Response.Write("<Script language='javascript'>alert('删除成功');</script>");

            Guid newsid = Guid.Empty;
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != string.Empty)
            {
                newsid = new Guid(Request.QueryString["id"]);
            }
            XPNews nm = new XPNews();
            XPNewsInfo n = nm.GetXPNews(newsid);
            bindToComment(newsid);

        }
        else
        {
            //Response.Write("<Script language='javascript'>alert('删除失败');</script>");
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<Script language='javascript'>alert('删除失败,请联系管理员');</script>", true);
        }


    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //string ckcode = txtCode.Text;
        //if (string.IsNullOrEmpty(ckcode)||(ckcode.ToUpper()!=Session["Code"].ToString()))
        //{
        //    Response.Write("<Script language='javascript'>alert('验证码错误');</script>");
        //    Response.Write(ckcode.ToUpper() + "<br>" + Session["Code"]);
        //    return;
        //}
        if (!Page.IsValid)
        {
            return;
        }
        //提交评论
        Guid newsid = Guid.Empty;
        if (Request.QueryString["id"] != null && Request.QueryString["id"] != string.Empty)
        {
            newsid = new Guid(Request.QueryString["id"]);
        }
        if (newsid == Guid.Empty)
        {
            return;
        }
        string commentContent = txtComment.Text.Trim();
        if (string.IsNullOrEmpty(commentContent))
        {
            Response.Write("<Script language='javascript'>alert('评论内容不能为空');</script>");
            return;
        }


        XPNewsComment cm1 = new XPNewsComment();
        XPNewsCommentInfo c = new XPNewsCommentInfo();
        c.NewsID = newsid;
        c.UserIP = Request.UserHostAddress;
        c.Content = commentContent;
        if (cm1.Insert(c)==0)
        {
            // Response.Write("<Script language='javascript'>alert('提交评论成功！');</script>");
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<Script language='javascript'>alert('提交评论成功！');</script>");
            txtComment.Text = "";
            txtCode.Text = "";
            bindToComment(newsid);
        }
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        string ckcode = txtCode.Text.Trim().ToUpper();
        string rightCode = Session["Code"].ToString();

        if (ckcode != rightCode)
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }

    }

    //根据session的值显示或隐藏删除评论的按钮
    protected void rptComment_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType== ListItemType.Item||e.Item.ItemType== ListItemType.AlternatingItem)
        {//当绑定项模板时
            if (Session["admin"]!=null && Session["admin"].ToString()=="admin")
            {
                ((LinkButton)e.Item.FindControl("lbtnDel")).Visible = true;

            }
                
        }
    }
}

